<template>
  <div class="app-container" style="padding:0">
      <div style="margin-top:20px">
        <el-button @click="showPop('add')" type="primary" size="small">新增</el-button>
      </div>
      <!--数据列表-->
      <div class="table-container">
        <el-table
          v-loading="listLoading"
          :data="tableData"
          border
          style="width: 100%; margin-top: 20px">
          <el-table-column  label="产品线标准价"  align="center" min-width="100">
            <template slot-scope="scope">
              {{scope.row.goodsLine}}
            </template>
          </el-table-column>
          <el-table-column align="center" label="公式" min-width="200">
            <template slot-scope="scope">
              {{scope.row.fFormula}}
            </template>
          </el-table-column>

          <el-table-column
            fixed="right"
            width="150"
            label="操作"
            align="center">
            <template slot-scope="scope">
              <el-button type="text" size="mini" @click="showPop('edit',scope.row)">编辑</el-button>
              <el-button type="text" size="mini" @click="delRow(scope.row)">删除</el-button>
            </template>
          </el-table-column>

        </el-table>
      </div>
      <div class="pagination-container">
        <el-pagination
          background
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          layout="total, sizes,prev, pager, next,jumper"
          :current-page.sync="listQuery.pageNum"
          :page-size="listQuery.pageSize"
          :page-sizes="[10,15,20,50,100]"
          :total="count">
        </el-pagination>
      </div>


    <!--公式设置-->
    <el-dialog
      title="公式设置"
      :visible.sync="dialogVisible"
      width="420px">
      <el-form :model="form" size="mini" label-width="100px" ref="form" :rules="rules">
        <el-form-item label="产品线：" prop="goodsLine">
            <el-input v-model="form.goodsLine"></el-input>
        </el-form-item>
        <el-form-item label="公式：" prop="fFormula">
          <el-input type="textarea" :rows="2" v-model="form.fFormula"></el-input>
          <div style="color:red;font-weight: bold;margin-top:10px">公式示例：【石油】+【苯】*2+2000</div>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
            <el-button @click="handleClose">取 消</el-button>
            <el-button type="primary" @click="handleConfirm" :loading="btnLoading">确 定</el-button>
        </span>
    </el-dialog>

  </div>
</template>
<script>

import {getOrderUsersList,} from '@/api/partner'
import {editFormula,priceFormulaList,deletePriceFormu} from '@/api/company'
import {formatDate} from '@/utils/date';

const defaultListQuery={
  "pageNum" : 1,
  "pageSize" : 10
};
const defaultForm = {
  goodsLine: '',
  fFormula: '',
  fId:0,
}

export default {
  name: "market",
  data(){
    return {
      listQuery: JSON.parse(JSON.stringify(defaultListQuery)),
      tableData: [],
      count: 0,
      listLoading: false,
      keyword:'',
      dialogVisible:false,
      btnLoading:false,
      form:JSON.parse(JSON.stringify(defaultForm)),
      rules:{
        goodsLine : [{required: true, message: '此项必填', trigger: 'blur'},],
        fFormula:[{required: true, message: '此项必填', trigger: 'blur'},],

      },
      popType:'',

    }
  },

  computed: {

  },
  created() {


  },
  methods:{
    //删除
    delRow(item){
      this.$confirm('确定删除此条记录?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deletePriceFormu({fId:item.fId}).then(()=>{
          this.getList();
          this.$message({
            type: 'success',
            message: '删除成功!'
          });
        });
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    init(){
      this.getList()
    },
    showPop(type,item){
      this.popType = type;
      this.dialogVisible = true;
      this.form = JSON.parse(JSON.stringify(defaultForm))
      if(item){
        this.form.fId = item.fId;
        this.form.goodsLine = item.goodsLine
        this.form.fFormula = item.fFormula
      }
    },
    handleConfirm(){

      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.btnLoading = true;
          let data = JSON.parse(JSON.stringify(this.form));
          editFormula(data).then((res)=>{
            this.btnLoading = false;
            this.dialogVisible = false;
            this.getList();
          }).catch(()=>{
            this.btnLoading = false;
          })
        }
      });
    },

    handleClose(){
      this.dialogVisible = false
    },


    handleSizeChange(val){
      this.listQuery.pageNum = 1;
      this.listQuery.pageSize = val;
      this.getList();
    },
    handleCurrentChange(val){
      this.listQuery.pageNum = val;
      this.getList();
    },

    handleSearchList(){
      this.listQuery.pageNum = 1;
      this.getList();
    },

    //获取列表
    getList() {
      this.listLoading = true;
      priceFormulaList(this.listQuery).then(response => {
          this.tableData = response.list;
          this.count = response.count;
          this.listLoading = false;
        }).catch(()=>{
          this.listLoading = false;
        });
    },
  },
}
</script>

<style scoped lang="scss">
    .goodsSn-item{
        text-align: center;
        font-size:12px;
        line-height: 20px;
        padding:8px;
        border-bottom:1px solid #eee;
        cursor: pointer;
    }
    .choose.goodsSn-item{
      background: #409eff;
      color:#fff;
    }
</style>
